package org.ucas.faker.web.controller.visualization;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jboss.jandex.ClassType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.ucas.faker.web.Config.VisualConfig;
import org.ucas.faker.web.controller.BaseController;
import org.ucas.faker.web.dto.CarryDto;
import org.ucas.faker.web.entity.patents.Patent;
import org.ucas.faker.web.fetcher.FetchExecutor;
import org.ucas.faker.web.fetcher.FetchReqBuilder;
import org.ucas.faker.web.fetcher.FetchRequest;
import org.ucas.faker.web.fetcher.FetchResult;
import org.ucas.faker.web.service.Visualization.DataService;
import org.ucas.faker.web.service.Visualization.OptionService;
import org.ucas.faker.web.service.Visualization.VisualService;
import org.ucas.faker.web.service.Visualization.entity.VisualParam;
import org.ucas.faker.web.service.Visualization.json.CloneUtil;
import org.ucas.faker.web.service.Visualization.json.DBUtil;
import org.ucas.faker.web.service.Visualization.json.JsonUtil;

import java.awt.*;
import java.lang.reflect.Constructor;
import java.util.*;

@RestController
@ResponseBody
public class VisualController extends BaseController {

    @Autowired private FetchExecutor fetcher;
    @Autowired private VisualService visualService;
    @Autowired private OptionService optionService;
    @Autowired private DataService dataService;

    @RequestMapping(value = "/graph/draw/option", method = RequestMethod.POST)
    @ResponseBody
    public CarryDto<String> graphRequest(@RequestParam(value = "graph_message", required = true) String json_string){
        System.out.println("graphRequest begins...");
        long depotId = currentDepotId();

        JSONObject inputJson = JSONObject.parseObject(json_string);
        JSONObject outputJson = new JSONObject();

        //解析参数
        String graphName = JsonUtil.getValue(inputJson, "graph_name");
        String basisName = JsonUtil.getValue(inputJson, "basis.name");
        String basisUnit = JsonUtil.getValue(inputJson, "basis.unit");
        JSONArray targetArray = JsonUtil.getValue(inputJson, "target");

        if (graphName == null || basisName == null || targetArray == null){
            fail(outputJson, "缺少graph_name、basis.name或target参数");
            return regularJson(outputJson);
        }
        if (targetArray.size()==0){
            fail(outputJson, "target长度至少为1");
            return regularJson(outputJson);
        }

        VisualParam param = new VisualParam();
        param.setGraphName(graphName);
        param.setBasisName(basisName);
        if (basisUnit != null){
            param.setBasisUnit(basisUnit);
        }
        param.setDepotId(depotId);

        FetchReqBuilder requestBuilder = FetchReqBuilder.instance().depot(depotId);

        dataService.parseBasisDimension(inputJson, param);
        dataService.parseConstraint(inputJson, requestBuilder);
        dataService.parseTarget(inputJson, param);

        switch (param.getBasisDimension()){
            case 2:
                switch (param.getTargetDimension()){
                    case 1:
                        dataService.parseData1D1T(inputJson, requestBuilder, param);
                        break;
                    case 2:
                        dataService.parseData1D2T(inputJson, requestBuilder, param);
                        break;
                    default:
                        break;
                }
                break;
            case 3:
                dataService.parseData2D1T(inputJson, requestBuilder, param);
                break;
            default:
                break;
        }

        //填充option
        String[] typeset = getGraphTypeSet(param);
        JSONArray valueJsonArray = new JSONArray();
        for (String thisType : typeset) {
            param.setGraphType(thisType);
            JSONObject valueJson = new JSONObject();
            valueJson.put("type", VisualConfig.getTypeCN(thisType));
            valueJson.put("option", optionService.getOption(param));
            valueJsonArray.add(valueJson);
        }
        outputJson.put("value", valueJsonArray);
        success(outputJson);

        return regularJson(outputJson);
    }

    private String[] getGraphTypeSet(VisualParam param){
        String[] typeset = new String[0];
        if (param.getBasisDimension() == 2){
            if (param.getTargetDimension() == 1) {
                typeset = VisualConfig.TYPESET_2D;
            } else if (param.getTargetDimension() == 2){
                typeset = VisualConfig.TYPESET_MultiTarget;
            }
        } else if (param.getBasisDimension() == 3){
            typeset = VisualConfig.TYPESET_3D;
        }
        return typeset;
    }

    private CarryDto<String> regularJson(JSONObject json){
        String tmp = json.toJSONString().replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2");
        tmp = JsonUtil.formatJson(tmp);
//        System.out.println(tmp);
        return CarryDto.<String>of(tmp);
    }

    private void success(JSONObject json){
        json.put("status", true);
        json.put("message", "可视化图生成成功！");
    }

    private void fail(JSONObject json, String message){
        json.put("status", true);
        json.put("message", message);
    }
}